﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Assignment_9
{
    public partial class Form1 : Form
    {
        private static ConnectDB cDb;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cDb = new ConnectDB();
            cDb.loadGrid(this.dgvPatient);
            cDb.loadComboboxSort(cboSort);
            cboCountry.SelectedIndex = 0;
            cboTest.SelectedIndex = 0;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (txtSearchName.Text.Trim() == "")
            {
                return;
            }
            
            string name = txtSearchName.Text.Trim();
            cDb.Search(name);
        }

        private void btnShowAll_Click(object sender, EventArgs e)
        {
            cDb.loadGrid(this.dgvPatient);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            cDb.Update();
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int index = dgvPatient.SelectedRows[0].Index;
            if (index < 0)
            {
                return;
            }

            if (MessageBox.Show("Bạn chắc chắn muốn xóa dòng này chứ?", "BIOMEDIC SYSTEM", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
            {
                return;
            }

            cDb.Delete(index);
        }

        private void cboSort_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboSort.SelectedIndex == 0)
            {
                return;
            }
            string sortBy = cboSort.SelectedItem.ToString().Trim();
            cDb.Sort(sortBy);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!ValidateForm())
            {
                return;
            }

            if(cDb.Insert(GetDataInput()))
            {
                ResetForm();
            }
            
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            ResetForm();
        }

        private void ResetForm()
        {
            txtId.Text = "";
            txtName.Text = "";
            txtAge.Text = "";
            cboCountry.SelectedIndex = 0;
            cboTest.SelectedIndex = 0;
            dtpDateTest.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
            dtpDateReport.Value = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
        }

        private Boolean ValidateForm()
        {
            if (txtId.Text.Trim() == "")
            {
                lblCheckId.Visible = true;
                txtId.Focus();
                return false;
            }
            lblCheckId.Visible = false;

            if (txtName.Text.Trim() == "")
            {
                lblCheckName.Visible = true;
                txtName.Focus();
                return false;
            }
            lblCheckName.Visible = false;

            if (txtAge.Text.Trim() == "")
            {
                lblCheckAge.Visible = true;
                txtAge.Focus();
                return false;
            }
            lblCheckAge.Visible = false;

            return true;
        }

        private List<object> GetDataInput()
        {
            List<object> lstData = new List<object>();
            lstData.Add(txtId.Text.Trim());
            lstData.Add(txtName.Text.Trim());
            lstData.Add(txtAge.Text.Trim());
            lstData.Add(cboCountry.SelectedItem.ToString());
            lstData.Add(cboTest.SelectedItem.ToString());
            lstData.Add(dtpDateTest.Text);
            lstData.Add(dtpDateReport.Text);

            return lstData;
        }
    }
}
